package cn.edu.zafu.bigdata.zengyadian;


import java.io.*;
        import java.util.ArrayList;
        import java.util.List;
        import java.util.Scanner;

class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    String id;
    String name;
    String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "ID: " + id + ", Name: " + name + ", Phone: " + phone;
    }
}

public class StudentManagementSystem {
    private static final String FILE_NAME = "students.ser";
    private List<Student> students = new ArrayList<>();

    public StudentManagementSystem() {
        loadStudents();
    }

    public void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            students = (List<Student>) ois.readObject();
        } catch (FileNotFoundException e) {
            System.out.println("No existing student file found. Starting with an empty list.");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("====学生信息管理系统====");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号(1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    searchStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    System.out.println("退出程序");
                    saveStudents();
                    return;
                default:
                    System.out.println("无效的选项，请重新输入。");
            }
        }
    }

    private void addStudent(Scanner scanner) {
        System.out.print("输入学号: ");
        String id = scanner.nextLine();
        System.out.print("输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("输入电话: ");
        String phone = scanner.nextLine();

        if (students.stream().anyMatch(s -> s.id.equals(id))) {
            System.out.println("学号重复，请重新输入。");
            return;
        }

        Student newStudent = new Student(id, name, phone);
        students.add(newStudent);
        System.out.println("学生信息已添加。");
        saveStudents();
    }

    private void searchStudent(Scanner scanner) {
        System.out.print("输入学号进行检索: ");
        String id = scanner.nextLine();
        Student foundStudent = students.stream().filter(s -> s.id.equals(id)).findFirst().orElse(null);

        if (foundStudent != null) {
            System.out.println("找到学生信息: " + foundStudent);
        } else {
            System.out.println("未找到该学生。");
        }
    }

    private void updateStudent(Scanner scanner) {
        System.out.print("输入学号进行更新: ");
        String id = scanner.nextLine();
        Student studentToUpdate = students.stream().filter(s -> s.id.equals(id)).findFirst().orElse(null);

        if (studentToUpdate == null) {
            System.out.println("未找到该学生。");
            return;
        }

        System.out.print("输入新的姓名: ");
        String name = scanner.nextLine();
        System.out.print("输入新的电话: ");
        String phone = scanner.nextLine();
        studentToUpdate.name = name;
        studentToUpdate.phone = phone;
        System.out.println("学生信息已更新。");
        saveStudents();
    }

    private void deleteStudent(Scanner scanner) {
        System.out.print("输入学号进行删除: ");
        String id = scanner.nextLine();
        boolean removed = students.removeIf(s -> s.id.equals(id));

        if (removed) {
            System.out.println("学生信息已删除。");
        } else {
            System.out.println("未找到该学生。");
        }
        saveStudents();
    }

    public static void main(String[] args) {
        StudentManagementSystem sms = new StudentManagementSystem();
        sms.run();
    }
}